import Vue from 'vue'
import Router from 'vue-router'
import Landing from '../components/Landing'
import Login from '../components/Login'
import Register from '../components/Register'
import Dashboard from '../components/Dashboard'
import CreateProfile from '../components/CreateProfile'
Vue.use(Router)

const router = new Router({
  mode: "history",
  linkActiveClass: "active",
  routes: [
    { path: '*', redirect: "/" },
    { path: '/', component: Landing },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/dashboard', component: Dashboard },
    { path: '/create-profile', component: CreateProfile },
  ]
})
// 全局守卫
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.jwtToken ? true : false;
  if (to.path == '/login' || to.path == '/register' || to.path == '/') {
    next();
  } else {
    isLogin ? next() : next('/login');   
  }
})


export default router